package com.bjsxt.userCenter.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userCenter.users.dao.IAAdminsDao;
import com.bjsxt.userCenter.users.dao.IARoleDao;
import com.bjsxt.userCenter.users.pojo.AAdmins;
import com.bjsxt.userCenter.users.pojo.ARole;
import com.bjsxt.userCenter.users.service.IUserService;
import com.bjsxt.usercenter.common.util.ConstatFinalUtil;
import com.bjsxt.usercenter.common.util.PageInfoUtil;

/**
 * 用户相关的service实现类
 * 
 * @author Administrator
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUserService
{

	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{

		return this.adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataJSON = new JSONObject();
		int res = this.adminsDao.save(admins);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}

		return resultJSON;
	}

	@Override
	public JSONObject updateAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataJSON = new JSONObject();
		int res = this.adminsDao.update(admins);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}

		return resultJSON;
	}

	@Override
	public JSONObject deleteAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataJSON = new JSONObject();
		int res = this.adminsDao.delete(admins);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}

		return resultJSON;
	}

	@Override
	public List<AAdmins> findListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		if (condMap.get("keyword") != null && !"".equals(condMap.get("keyword")))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		if (pageInfoUtil != null)
		{
			/* 分页 */
			/* 总记录数 */
			condMap.put("pageCount", "true");
			/* 取总记录数 */
			List<AAdmins> adminsList = this.adminsDao.findList(condMap);
			if (adminsList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			}
			condMap.put("pageCount", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findList(condMap);
		}
		return this.adminsDao.findList(condMap);
	}

	/*
	 * ====================================ROLE=================================
	 * ===========
	 */
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.roleDao.findOne(condMap);
	}

	@Override
	public List<ARole> findListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if (pageInfoUtil != null)
		{
			condMap.put("pageConut", "true");
			List<ARole> roleList = this.roleDao.findList(condMap);
			if (roleList.size()==1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			condMap.put("pageConut", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			
			return this.roleDao.findList(condMap);
		}
		return this.roleDao.findList(condMap);
	}

	@Override
	public JSONObject saveRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataJSON = new JSONObject();
		int res = this.roleDao.save(role);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			dataJSON.put("effect", res);
			dataJSON.put("id", role.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}

		return resultJSON;
	}

	@Override
	public JSONObject updateRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataJSON = new JSONObject();
		int res = this.roleDao.update(role);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			dataJSON.put("effect", res);
			dataJSON.put("id", role.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}

		return resultJSON;
	}

	@Override
	public JSONObject deleteRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataJSON = new JSONObject();
		int res = this.roleDao.delete(role);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			dataJSON.put("effect", res);
			dataJSON.put("id", role.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}

		return resultJSON;
	}

}
